package com.web.boot.web.auth;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.GetModel;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.facade.MenuFacade;
import com.web.base.auth.facade.RoleFacade;
import com.web.base.auth.message.AppResponse;
import com.web.base.auth.message.RoleMenuRequest;
import com.web.base.auth.message.RoleRequest;
import com.web.base.auth.message.RoleResponse;
import com.web.boot.global.model.App;
import com.web.boot.global.util.HttpContext;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

@Controller("authRoleController")
@Http(url = "/auth")
public class RoleController
{
	@Autowired
	private RoleFacade roleFacade;
	@Autowired
	private MenuFacade menuFacade;
	@Resource
	private AppFacade appFacade;
	@Resource
	private HttpContext httpContext;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public RoleController()
	{
	}
	
	@Http(url = "/role_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		RoleRequest roleRequest = request.getQueryObject(RoleRequest.class);
		List<AppResponse> appList = appFacade.listAll();
		AppResponse app = null;
		if (roleRequest.getAppId() == null && !appList.isEmpty())
		{
			app = appList.get(0);
			roleRequest.setAppId(app.getId());
		}
		else if (roleRequest.getAppId() != null)
		{
			app = appFacade.get(roleRequest.getAppId());
		}
		httpContext.setApp(request, App.toApp(app));
		return View.createBuilder().setView("/auth/role_list")
				.put("appList", appList)
				.put("app", app)
				.put("model", request)
				.put("map", dictParser.getStringMap())
				.put("page", roleFacade.listPage(roleRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/role_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		RoleRequest roleRequest = request.getPostObject(RoleRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/auth/role_list_grid")
				.put("page", roleFacade.listPage(roleRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/role_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		RoleResponse response = null;
		App app = httpContext.getApp(request);
		if (id > 0)
		{
			response = roleFacade.get(id);
		}
		return View.createBuilder().setView("/auth/role_get")
				.put("app", app)
				.put("model", response == null ? new RoleResponse() : response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/role_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		RoleRequest roleRequest = request.getPostObject(RoleRequest.class);
		RoleResponse response = null;
		if (roleRequest.getId() == null)
		{
			ValidationUtil.valid(roleRequest, SaveModel.class);
			response = roleFacade.add(roleRequest);
		}
		else
		{
			ValidationUtil.valid(roleRequest, UpdateModel.class);
			response = roleFacade.update(roleRequest);
		}
	}
	
	@Http(url = "/role_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		roleFacade.refresh();
	}
	
	@Http(url = "/role_menu_get", method = HttpMethod.GET)
	public View getMenu(HttpRequest request)
	{
		RoleRequest roleRequest = request.getQueryObject(RoleRequest.class, GetModel.class);
		return View.createBuilder().setView("/auth/role_menu_get")
				.put("app", httpContext.getApp(request))
				.put("model", roleFacade.get(roleRequest.getId()))
				.put("menuList", menuFacade.listSelect(roleRequest.getId()))
				.build();
	}
	
	@Http(url = "/role_menu_save", method = HttpMethod.POST)
	public void saveMenu(HttpRequest request)
	{
		RoleMenuRequest roleMenuRequest = request.getPostObject(RoleMenuRequest.class, SaveModel.class);
		menuFacade.addRoleMenu(roleMenuRequest);
	}
	
}
